//
//  HSDSpidermanCache.m
//  GoldenCard
//
//  Created by 正合适 on 2018/3/21.
//  Copyright © 2018年 合时代. All rights reserved.
//

#import "HSDSpidermanCache.h"
#import <FMDB/FMDB.h>

static NSString *kPageJsonTable = @"spidermanPagejsontable";

static NSString *kResultJsonTable = @"spidermanResultJsonTable";

static NSString *kDBName = @"SpidermanDB";

static NSString *dbPath() {
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *file = [doc stringByAppendingPathComponent:[NSString stringWithFormat:@"HSD_%@.sqlite", kDBName]];
    return file;
}

@interface HSDSpidermanCache ()
/**<#desc#>*/
@property (nonatomic, strong) FMDatabaseQueue *dbQueue;
/**<#desc#>*/
@property (nonatomic, strong) FMDatabase *db;
@end

@implementation HSDSpidermanCache
#pragma mark - 单例
+ (instancetype)shareInstance {
    static HSDSpidermanCache *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HSDSpidermanCache alloc] initWithDataBaseName:kDBName];
    });
    return instance;
}

#pragma mark - 初始化数据库
- (instancetype)initWithDataBaseName:(NSString *)dbName {
    self = [super init];
    if (self) {
        _db = [FMDatabase databaseWithPath:dbPath()];
        if ([_db open]) {
            [self createTable];
            _dbQueue = [FMDatabaseQueue databaseQueueWithPath:dbPath()];
        } else {
        }
    }
    return self;
}

#pragma mark - 创建数据表
- (void)createTable {
    BOOL result = NO;
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (id integer primary key autoincrement  not null, jsonval text not null);",kPageJsonTable];
    NSString *createResultTablesql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (id integer primary key autoincrement  not null, jsonval text not null);",kResultJsonTable];
    result = [_db executeUpdate:sql];
    [_db executeUpdate:createResultTablesql];
}

#pragma mark - 插入数据
- (void)insertPageWithVal:(NSString *)val {
    [_dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *sql = [NSString stringWithFormat:@"insert into %@ (jsonval) VALUES (?);",kPageJsonTable];
        [db executeUpdate:sql,val];
    }];
}

- (void)insertResultWithVal:(NSString *)val {
    [_dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *sql = [NSString stringWithFormat:@"insert into %@ (jsonval) VALUES (?);",kResultJsonTable];
        [db executeUpdate:sql,val];
    }];
}

- (NSArray *)getAllPageVal {
    __block NSMutableArray *arr = [NSMutableArray array];
    [_dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@",kPageJsonTable];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:[NSString stringWithFormat:@"%d",[rs intForColumn:@"id"]] forKey:@"id"];
            [dic setObject:[rs stringForColumn:@"jsonval"] forKey:@"json"];
            [arr addObject:dic];
        }
    }];
    return arr;
}

- (NSArray *)getAllResultVal {
    __block NSMutableArray *arr = [NSMutableArray array];
    [_dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@",kResultJsonTable];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:[NSString stringWithFormat:@"%d",[rs intForColumn:@"id"]] forKey:@"id"];
            [dic setObject:[rs stringForColumn:@"jsonval"] forKey:@"json"];
            [arr addObject:dic];
        }
    }];
    return arr;
}

- (NSString *)getFirstPageVal {
    __block NSString *val = @"";
    [_dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *sql = [NSString stringWithFormat:@"select * from %@ limit 1",kPageJsonTable];
        FMResultSet *rs = [db executeQuery:sql];
        while ([rs next]) {
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            [dic setObject:[NSString stringWithFormat:@"%d",[rs intForColumn:@"id"]] forKey:@"id"];
            [dic setObject:[rs stringForColumn:@"jsonval"] forKey:@"json"];
            val = [rs stringForColumn:@"jsonval"];
        }
    }];
    return val;
}

- (void)deletePageValWithId:(NSString *)idStr {
    [_dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@ where id = ?",kPageJsonTable];
        [db executeUpdate:sql,idStr];
    }];
}

- (void)deletePageValWithVal:(NSString *)val {
    [_dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@ where jsonval = ?",kPageJsonTable];
        [db executeUpdate:sql,val];
    }];
}

- (void)deleteResultValWithVal:(NSString *)val {
    [_dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
        NSString *sql = [NSString stringWithFormat:@"delete from %@ where jsonval = ?",kResultJsonTable];
        [db executeUpdate:sql,val];
    }];
}


@end
